package com.maxwell.googleplay.base;

import com.maxwell.googleplay.R;
import com.maxwell.googleplay.factory.ThreadPoolFactory;
import com.maxwell.googleplay.utils.LogUtils;
import com.maxwell.googleplay.utils.UIUtils;

import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;

/**
 * @author yue
 * @创建时间 2016-3-30上午11:37:50
 * @描述 TODO
 * 
 */
public abstract class LoadingPager extends FrameLayout {

	public static final int STATE_NONE = -1;
	public static final int STATE_LOADING = 0;//正在请求网络
	public static final int STATE_EMPTY = 1;//空状态
	public static final int STATE_ERROR = 2;//错误状态
	public static final int STATE_SUCCESS = 3;//成功状态

	public int mCurState = STATE_NONE;
	
	private View mLoadingView;
	private View mErrorView;
	private View mEmptyView;
	private View mSuccessView;
	

	public LoadingPager(Context context) {
		super(context);
		initCommonView();
	}

	/**
	 * @des 初始化常规视图
	 * @call LoadingPager初始化的时候被调用
	 */
	private void initCommonView() {
		// 1.加载页面

		mLoadingView = View.inflate(UIUtils.getContext(),
				R.layout.pager_loading, null);
		this.addView(mLoadingView);
		// 2.错误页面

		mErrorView = View.inflate(UIUtils.getContext(), R.layout.pager_error,
				null);
		this.addView(mErrorView);
		// 3.空页面

		mEmptyView = View.inflate(UIUtils.getContext(), R.layout.pager_empty,
				null);
		this.addView(mEmptyView);

		refreshUI();
	}

	/**
	 * @des 根据当前状态显示不同view
	 * @call LoadingPager初始化的时候被调用
	 * @call 真正加载数据执行完成之后被调用
	 */
	private void refreshUI() {
		/*
		 * if(mCurState == STATE_LOADING){ //显示对应视图
		 * mLoadingView.setVisibility(View.VISIBLE); }else{
		 * mLoadingView.setVisibility(View.GONE); }
		 */
		// 控制loading视图
		mLoadingView.setVisibility((mCurState == STATE_LOADING)||(mCurState == STATE_NONE) ? View.VISIBLE
				: View.GONE);
		// 控制error视图
		mErrorView.setVisibility((mCurState == STATE_ERROR) ? View.VISIBLE
				: View.GONE);
		mErrorView.findViewById(R.id.error_btn_retry).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//重新触发加载数据
				loadData();			
			}
		});
		
		
		// 控制empty视图
		mEmptyView.setVisibility((mCurState == STATE_EMPTY) ? View.VISIBLE
				: View.GONE);
		//
		if(mSuccessView == null && mCurState == STATE_SUCCESS){
			//创建成功视图
			mSuccessView = initSuccessView();
			//
			this.addView(mSuccessView);
		}
		if(mSuccessView != null){
			mSuccessView.setVisibility((mCurState == STATE_SUCCESS) ? View.VISIBLE
					: View.GONE);
		}

	}

	

	/**
	 * @desc 触发加载数据
	 * @call 暴露给外界调用,其实就是外界触发加载数据
	 */
	public void loadData() {
		//如果加载成功过，就不要再次加载
		if(mCurState != STATE_SUCCESS && mCurState != STATE_LOADING){
			
			//保证每次执行的时候一定是加载中的视图,而不是上次的状态
			int state = STATE_LOADING;
			mCurState = state;
			refreshUI();
			// 异步加载数据
			//new Thread(new LoadDataTask()).start();
			ThreadPoolFactory.getNormalPool().execute(new LoadDataTask());
		}
		
		
	}

	class LoadDataTask implements Runnable {

		@Override
		public void run() {
			// 真正的去加载网络数据
			LoadedResult tempState = initData();
			// 处理加载结果
			mCurState = tempState.getState();
			//刷新UI		
			UIUtils.postTaskSafely(new Runnable(){
				@Override
				public void run() {
					refreshUI();					
				}
			});
		}

	}

	/**
	 * @desc 必须实现，但是不知道具体实现，定义成抽象方法，让子类实现
	 * @call loadData()方法调用的时候
	 */
	public abstract LoadedResult initData();
	
	/**
	 * @return 返回成功视图
	 * @call 真正加载数据执行完成之后被调用并且数据加载成功，必须告知具体的成功视图
	 */
	public abstract View initSuccessView();
	
	
	public enum LoadedResult{
		SUCCESS(STATE_SUCCESS), ERROR(STATE_ERROR), EMPTY(STATE_ERROR);
		int state;
		public int getState(){
			return state;
		}
		
		private LoadedResult(int state){
			this.state = state;
		}
	}
	
}
